home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte19 / getpos.c < prev    next >
C/C++ Source or Header  |  1996-04-29  |  8KB  |  283 lines

  1.  
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include "GetPos.h"
  5. #include <vfw.h>
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "My Application"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.     WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );
  106. }
  107.  
  108. HWND       hMCIWnd  = NULL;   // MCIWnd window handle
  109.  
  110.  
  111. LONG nStartSel = 0;
  112. LONG nEndSel   = 0;
  113.  
  114.  
  115. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  116. {
  117.    switch( uMsg )
  118.    {
  119.       case WM_CREATE :
  120.               // create MCIWnd window
  121.               //.....................
  122.  
  123.               hMCIWnd = MCIWndCreate(hWnd, hInst, 
  124.                                      WS_VISIBLE | 
  125.                                      WS_CHILD | 
  126.                                      WS_OVERLAPPED |
  127.                                      WS_CAPTION |
  128.                                      WS_BORDER |
  129.                                      MCIWNDF_NOTIFYMODE |
  130.                                      MCIWNDF_SHOWALL | 
  131.                                      MCIWNDF_NOPLAYBAR, 
  132.                                      NULL
  133.                                     );
  134.                           
  135.               if (!hMCIWnd)
  136.               {
  137.                   MessageBox(hWnd, "Error creating MCIWnd window...", 
  138.                              NULL, MB_OK);
  139.                   DestroyWindow( hWnd );
  140.               }
  141.               break;
  142.  
  143.       case WM_COMMAND :
  144.               switch( LOWORD( wParam ) )
  145.               {
  146.                  // file commands
  147.                  //..............
  148.  
  149.                  case IDM_OPEN_DIALOG :
  150.                          if ( MCIWndOpenDialog(hMCIWnd) )
  151.                              MCIWndValidateMedia(hMCIWnd);
  152.                          break;
  153.                          
  154.                  case IDM_CLOSE :
  155.                          MCIWndClose(hMCIWnd);
  156.                          break;
  157.  
  158.                  // playback
  159.                  //.........
  160.                                            
  161.                  case IDM_PLAY :
  162.                          MCIWndPlay(hMCIWnd);
  163.                          break;
  164.                                    
  165.                  case IDM_STOP :
  166.                          MCIWndStop(hMCIWnd);
  167.                          break;
  168.                                            
  169.                  case IDM_REPEAT :
  170.                          // toggle repeat
  171.                          MCIWndSetRepeat(hMCIWnd, !MCIWndGetRepeat(hMCIWnd) );
  172.                          break;
  173.                  
  174.                  // mark selection
  175.                  //...............
  176.  
  177.                  case IDM_START_SEL :
  178.                          {
  179.                             nStartSel = MCIWndGetPosition(hMCIWnd);
  180.  
  181.                             if (nStartSel > nEndSel)
  182.                                 nEndSel = nStartSel;
  183.                          }     
  184.                          break;
  185.                  
  186.                  case IDM_END_SEL :
  187.                          {
  188.                             nEndSel = MCIWndGetPosition(hMCIWnd);
  189.  
  190.                             if (nStartSel > nEndSel)
  191.                                 nStartSel = nEndSel;
  192.                          }
  193.                          break;
  194.  
  195.                  // play selection
  196.                  //...............
  197.                                            
  198.                  case IDM_PLAY_SEL :
  199.                          if (nStartSel < nEndSel)
  200.                              MCIWndPlayFromTo(hMCIWnd, nStartSel, nEndSel);
  201.                          break;
  202.                  
  203.                  case IDM_PLAY_BEGIN_SEL :
  204.                          if (nStartSel)
  205.                          {
  206.                              MCIWndHome(hMCIWnd);
  207.                              MCIWndPlayTo(hMCIWnd, nStartSel);
  208.                          }
  209.                          break;
  210.                  
  211.                  case IDM_PLAY_SEL_END :
  212.                          if (nEndSel)
  213.                              MCIWndPlayFrom(hMCIWnd, nEndSel);
  214.                          else if (nStartSel)
  215.                              MCIWndPlayFrom(hMCIWnd, nStartSel);
  216.                          break;
  217.                  
  218.                  case IDM_GOTO_SEL :
  219.                          MCIWndSeek(hMCIWnd, nStartSel);
  220.                          break;
  221.  
  222.                  // other commands
  223.                  //...............
  224.                  
  225.                  case IDM_ABOUT :
  226.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  227.                         break;
  228.  
  229.                  case IDM_EXIT :
  230.                         DestroyWindow(hWnd);
  231.                         break;
  232.               }
  233.               break;
  234.  
  235.       case WM_DESTROY :
  236.               // destroy MCIWnd, if one exists
  237.               //..............................
  238.  
  239.               if (hMCIWnd)
  240.                   MCIWndDestroy(hMCIWnd);
  241.  
  242.               PostQuitMessage(0);
  243.               break;
  244.  
  245.       default :
  246.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  247.    }
  248.  
  249.    return( 0L );               
  250. }
  251.  
  252. LRESULT CALLBACK About( HWND hDlg,           
  253.                         UINT message,        
  254.                         WPARAM wParam,       
  255.                         LPARAM lParam)
  256. {
  257.    switch (message) 
  258.    {
  259.        case WM_INITDIALOG: 
  260.                return (TRUE);
  261.  
  262.        case WM_COMMAND:                              
  263.                if (   LOWORD(wParam) == IDOK         
  264.                    || LOWORD(wParam) == IDCANCEL)    
  265.                {
  266.                        EndDialog(hDlg, TRUE);        
  267.                        return (TRUE);
  268.                }
  269.                break;
  270.    }
  271.  
  272.    return (FALSE); 
  273. }
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.